package com.member.web.controller.shop;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.member.common.annotation.Anonymous;
import com.member.common.annotation.Log;
import com.member.common.constant.Constants;
import com.member.common.enums.BusinessType;
import com.member.common.utils.DateUtils;
import com.member.common.utils.StringUtils;
import com.member.common.utils.http.HttpUtils;
import com.member.shop.domain.SysBizGoods;
import com.member.shop.domain.SysBizOftenBuy;
import com.member.shop.domain.SysBizUser;
import com.member.shop.dto.ErpStockEdit;
import com.member.shop.dto.ExternalUserLikeDto;
import com.member.shop.dto.ExternalUserLikeProductDto;
import com.member.shop.service.ISysBizGoodsService;
import com.member.shop.service.ISysBizOftenBuyService;
import com.member.shop.service.ISysBizUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/*导老数据历史清单*/
//@Anonymous
@RestController
@RequestMapping("/shop/old")
public class OftenBuyExternal {

    @Resource
    private ISysBizUserService sysBizUserService;


    @Resource
    private ISysBizGoodsService sysBizGoodsService;


    @Resource
    private ISysBizOftenBuyService sysBizOftenBuyService;

    /**
     * 商品库存变动接口
     */
    @RequestMapping("/often")
    public void oftenBuyExternal() {
        String getUserUrlStr = HttpUtils.sendGet("http://mp.wxheyi.cn//UserMigration/GetUserLike?token=74be16979710d4c4e7c6647856088456");
        // 解析响应
        JSONObject jsonResponse = JSON.parseObject(getUserUrlStr);
        int code = jsonResponse.getIntValue("code");

        //老系统查到该用户 但是新系统没有认证过
        if (code == 200) {
            String jsonArrayString = jsonResponse.getString("data");
            List<ExternalUserLikeDto> userLikeDto = JSON.parseArray(jsonArrayString, ExternalUserLikeDto.class);

            if (!StringUtils.isEmpty(userLikeDto)) {
                for (ExternalUserLikeDto externalUserLikeDto : userLikeDto) {
                    if (StringUtils.isEmpty(externalUserLikeDto.getMobile())) {
                        continue;
                    }

                    List<ExternalUserLikeProductDto> proList = externalUserLikeDto.getProList();
                    if (CollectionUtils.isEmpty(proList)) {
                        continue;
                    }


                    List<SysBizUser> sysBizUsers = sysBizUserService.selectSysBizUserList(new LambdaQueryWrapper<SysBizUser>()
                            .eq(SysBizUser::getUserPhone, externalUserLikeDto.getMobile())
                            .eq(SysBizUser::getDeleted, 0)
                    );
                    if (StringUtils.isEmpty(sysBizUsers)) {
                        continue;
                    }
                    Long userId = sysBizUsers.get(0).getId();
                    Long tenantId = sysBizUsers.get(0).getTenantId();

                    //防止数据重复  和商品必须是无锡

                    List<SysBizOftenBuy> sysBizOftenBuys1 = new ArrayList<>();
                    for (ExternalUserLikeProductDto externalUserLikeProductDto : proList) {
                        String title = externalUserLikeProductDto.getTitle();
                        List<SysBizGoods> sysBizGoods = sysBizGoodsService.selectSysBizGoodsList(new LambdaQueryWrapper<SysBizGoods>()
                                .eq(SysBizGoods::getTenantId, tenantId)
                                .eq(SysBizGoods::getName, title)
                        );
                        if (CollectionUtils.isEmpty(sysBizGoods)) {
                            continue;
                        }

                        List<SysBizOftenBuy> sysBizOftenBuys = sysBizOftenBuyService.selectSysBizOftenBuyList(new LambdaQueryWrapper<SysBizOftenBuy>()
                                .eq(SysBizOftenBuy::getGoodsId, sysBizGoods.get(0).getId())
                                .eq(SysBizOftenBuy::getUserId, userId)
                                .eq(SysBizOftenBuy::getDeleted, 0)
                        );
                        if (!CollectionUtils.isEmpty(sysBizOftenBuys)) {
                            continue;
                        }

                        SysBizOftenBuy sysBizOftenBuy = new SysBizOftenBuy();

                        sysBizOftenBuy.setUserId(userId);

                        sysBizOftenBuy.setTenantId(tenantId);
                        sysBizOftenBuy.setGoodsId(sysBizGoods.get(0).getId());
                        sysBizOftenBuy.setAddTime(LocalDateTime.now());
                        sysBizOftenBuys1.add(sysBizOftenBuy);

                    }
                    if(!CollectionUtils.isEmpty(sysBizOftenBuys1)){
                        sysBizOftenBuyService.saveBatch(sysBizOftenBuys1);
                    }
                }
            }


        }
    }
}
